package com.hqyj.system.controller;

import com.hqyj.system.model.SysRole;
import com.hqyj.system.model.SysUser;
import com.hqyj.system.model.SysUserRole;
import com.hqyj.utils.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Auther: ZLF
 * @Date: 2021/12/30 10:51
 * @Description:
 */
@Controller
@RequestMapping("/sysRoleController")
public class SysRoleController extends BaseController {
    /**
     * 列表
     *
     * @param request
     * @param response
     * @param session
     * @param model
     * @param sysRole
     * @return
     * @throws IOException
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole) throws IOException {
        //1.接收数据
        //3.调用业务
        List<SysRole> sysRoleList_db = sysRoleService.queryAllSysRole();
        model.addAttribute("sysRoleList_db", sysRoleList_db);
        //2.跳转页面
        return "view/system/sysRole/sysRoleList";
    }


    @RequestMapping("/add")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole) throws IOException {
        //1.接收数据
        //3.调用业务
        int n = sysRoleService.addSysRoleBySysRole(sysRole);

        //2.跳转页面
        List<SysRole> sysRoleList_db = sysRoleService.queryAllSysRole();
        model.addAttribute("sysRoleList_db", sysRoleList_db);
        return "view/system/sysRole/sysRoleList";
    }

    @RequestMapping("/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String roleIdStr) throws IOException {
        //1.接收数据
        //3.调用业务
        //字符类型转数字类型
        int roleId = Integer.parseInt(roleIdStr);
        int n = sysRoleService.deleteSysRoleByRoleId(roleId);

        //2.跳转页面
        List<SysRole> sysRoleList_db = sysRoleService.queryAllSysRole();
        model.addAttribute("sysRoleList_db", sysRoleList_db);
        return "view/system/sysRole/sysRoleList";
    }

    @RequestMapping("/updateUI")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String roleIdStr) throws IOException {
        //1.接收数据
        //3.调用业务
        //字符类型转数字类型
        int roleId = Integer.parseInt(roleIdStr);
        SysRole sysRole_db = sysRoleService.querySysRoleByRoleId(roleId);
        model.addAttribute("sysRole_db", sysRole_db);

        //2.跳转页面
        return "view/system/sysRole/updateSysRole";
    }

    @RequestMapping("/update")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String roleIdStr) throws IOException {
        //1.接收数据
        int roleId = Integer.parseInt(roleIdStr);
        sysRole.setRoleId(roleId);
        //3.调用业务
        int n = sysRoleService.updateSysRoleBySysRole(sysRole);

        //2.跳转页面
        List<SysRole> sysRoleList_db = sysRoleService.queryAllSysRole();
        model.addAttribute("sysRoleList_db", sysRoleList_db);
        return "view/system/sysRole/sysRoleList";
    }

    @RequestMapping("/FPRoleUI")
    public String FPRoleUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String roleIdStr, String userIdStr) throws IOException {
        //1.接收数据
        int userId = Integer.parseInt(userIdStr);
        //3.调用业务
        //a.用户数据
        SysUser sysUser_db = sysUserService.querySysUserByUserId(userId);
        model.addAttribute("sysUser_db", sysUser_db);

        //b.分配角色数据
        List<SysRole> sysRoleList_db = sysRoleService.queryAllSysRole();
        model.addAttribute("sysRoleList_db", sysRoleList_db);

        //c.选中角色数据
        SysRole sysRole_xz_db = sysRoleService.querySysRoleByUserId(userId);
        model.addAttribute("sysRole_xz_db", sysRole_xz_db);
        //2.跳转页面
        return "view/system/sysRole/FPRoleUI";
    }

    @RequestMapping("/FPRole")
    public String FPRole(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRole sysRole, String roleIdStr, String userIdStr, SysUserRole sysUserRole) throws IOException {
        //1.接收数据
        int userId = Integer.parseInt(userIdStr);
        int roleId = Integer.parseInt(roleIdStr);
        //3.调用业务
        //-->中间表增加数据，先判断数据库有没有
        int m = sysUserRoleService.deleteSysUserRoleByUserId(userId);

        //增加
        sysUserRole.setUserId(userId);
        sysUserRole.setRoleId(roleId);
        int n = sysUserRoleService.addSysUserRoleBySysUserRole(sysUserRole);

        //2.跳转页面
        //a.用户数据
        SysUser sysUser_db = sysUserService.querySysUserByUserId(userId);
        model.addAttribute("sysUser_db", sysUser_db);
        //b.分配角色数据
        List<SysRole> sysRoleList_db = sysRoleService.queryAllSysRole();
        model.addAttribute("sysRoleList_db", sysRoleList_db);
        //c.选中角色数据
        SysRole sysRole_xz_db = sysRoleService.querySysRoleByUserId(userId);
        model.addAttribute("sysRole_xz_db", sysRole_xz_db);
        System.err.println("------------------------" + sysRole_xz_db);

        return "view/system/sysRole/FPRoleUI";
    }

}